
var publicApi = require('../../script/public.js');
var revenueApi = require('../../script/revenue.js');
var timer;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    UserList: [],
    SearchParams: {
      DEPT_NO: '',
      DEPT_NAME: '',
      LOU_NO: '',
      LOU_NAME: '',
      UNIT: '',
      CONS_NO: '',
      CONS_NAME: '',
      CONS_SORT_CODE: '',
      CONS_SORT_NAME: '',
      WATEROFF_CODE: '',
      WATEROFF_CODE_STR: '',
      PHONE: '',
      ISOWE: '',
      ISOWE_NAME: '',
    },
    pagination: {
      page: 1,
      rows: 30,
      sidx: 't.ORDER_CODE',
      sord: ''
    },
    HasNextPage: true,
    noData: false,
    isReachBottom: false,
    // 弹窗
    ShowPop: false,
    DeptShow: false,
    DeptList: [],
    DeptListShow: [],
    BuildingShow: false,
    BuildingList: [],
    BuildingListShow: [],
    UserTypeShow: false,
    UserTypeActive: 0,
    UserTypeList: [],
    UseHeatStateShow: false,
    UseHeatStateActive: 0,
    UseHeatState: [],
    IsOweShow: false,
    IsOweActive: 0,
    IsOweList: [{
        name: '不欠费',
        value: 0
      },
      {
        name: '欠费',
        value: 1
      }
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.GetPCodes();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this._refreshData();
    this.getDepts();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    wx.showNavigationBarLoading();
    this._refreshData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.setData({
      isReachBottom: true
    });
    if (this.data.HasNextPages) {
      this.setData({
        'pagination.page': this.data.pagination.page + 1
      })
      this.searchUser();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  _refreshData() {
    this.setData({
      pagination: {
        page: 1,
        rows: 30,
        sidx: 't.ORDER_CODE',
        sord: ''
      },
      SearchParams: {
        DEPT_NO: '',
        DEPT_NAME: '',
        LOU_NO: '',
        LOU_NAME: '',
        UNIT: '',
        CONS_NO: '',
        CONS_NAME: '',
        CONS_SORT_CODE: '',
        CONS_SORT_NAME: '',
        WATEROFF_CODE: '',
        WATEROFF_CODE_STR: '',
        PHONE: '',
        ISOWE: '',
        ISOWE_NAME: '',
      },
      HasNextPage: true,
      noData: false,
      isReachBottom: false,
      BuildingList: [],
    });
    this.searchUser(true);
  },
  GetPCodes() {
    publicApi.GetPCodes({
      keys: ['UserType', 'UseHeatState'],
    }).then(res => {
      this.setData({
        UserTypeList: res.data.UserType,
        UseHeatState: res.data.UseHeatState,
      })
    })
  },
  getDepts() {
    const params = {
      queryParam: {
        DropType: 1,
        Param_0: ''
      },
      pagination: {
        rows: 10000,
        page: 1,
        sidx: "",
        sord: ""
      }
    }
    publicApi.GetDropDownList_Page(params).then(res => {
      if (res.code === 200) {
        this.setData({
          DeptList: res.data.Items,
          DeptListShow: res.data.Items,
        });
      }
    })
  },
  getBuilding() {
    const params = {
      queryParam: {
        DropType: 3,
        Param_0: '',
        Param_1: this.data.SearchParams.DEPT_NO,
        Param_2: ""
      },
      pagination: {
        rows: 10000,
        page: 1,
        sidx: "",
        sord: ""
      }
    }
    publicApi.GetDropDownList_Page(params).then(res => {
      if (res.code === 200) {
        this.setData({
          BuildingList: res.data.Items,
          BuildingListShow: res.data.Items,
        });
      }
    })
  },
  openSearch() {
    this.setData({
      ShowPop: true
    });
  },
  leave() {
    this.setData({
      ShowPop: false
    });
  },
  searchUser(isRefresh) {
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    const params = {
      queryJson: {
        isAudit: true,
        DEPT_NO: this.data.SearchParams.DEPT_NO,
        LOU_NO: this.data.SearchParams.LOU_NO,
        UNIT: this.data.SearchParams.UNIT,
        CONS_NO: this.data.SearchParams.CONS_NO,
        CONS_NAME: this.data.SearchParams.CONS_NAME,
        CONS_SORT_CODE: this.data.SearchParams.CONS_SORT_CODE,
        WATEROFF: this.data.SearchParams.WATEROFF_CODE,
        PHONE: this.data.SearchParams.PHONE,
        ISOWE: this.data.SearchParams.ISOWE,
      },
      pagination: {
        ...this.data.pagination
      }
    }
    revenueApi.GetUserPageSelectForAudit(params).then(res => {
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
      wx.hideLoading();
      if (res.code === 200) {
        let dataList = []
        if (isRefresh) {
          dataList = [...res.data.rows]
        } else {
          dataList = [...this.data.UserList, ...res.data.rows]
        }
        const noData = dataList.length === 0
        this.setData({
          UserList: dataList,
          HasNextPages: this.data.pagination.page < res.data.total,
          noData: noData
        })
      }
    }).catch(_err => {
      wx.hideLoading();
    });
  },
  showPopup(e) {
    const popupType = e.currentTarget.dataset.pop;
    if (popupType === 'Building' && this.data.SearchParams.DEPT_NO === '') {
      wx.showToast({
        title: '请先选择小区',
        icon: 'none'
      });
      return;
    }
    this.setData({
      [popupType + 'Show']: true
    });
  },
  closePop(e) {
    const popupType = e.currentTarget.dataset.pop;
    this.setData({
      [popupType + 'Show']: false,
      searchText: '',
      DeptListShow: [...this.data.DeptList],
      BuildingListShow: []
    });
  },
  searchDeptChange(e) {
    let val = e.detail.value;
    clearTimeout(this.timer);
    this.timer = setTimeout(() => {
      const list = [...this.data.DeptList];
      const newList = list.filter(item => {
        return item.Label.indexOf(val) > -1
      })
      this.setData({
        searchText: val,
        DeptListShow: newList
      });
    })
  },
  addDept(e) {
    const dept = e.currentTarget.dataset.info;
    this.setData({
      'SearchParams.DEPT_NO': dept.ID,
      'SearchParams.DEPT_NAME': dept.Label,
      DeptShow: false
    });
    this.getBuilding();
  },
  searchBuildingChange(e) {
    let val = e.detail.value;
    clearTimeout(this.timer);
    this.timer = setTimeout(() => {
      const list = [...this.data.BuildingList];
      const newList = list.filter(item => {
        return item.Label.indexOf(val) > -1
      })
      this.setData({
        searchText: val,
        BuildingListShow: newList
      });
    })
  },
  addBuilding(e) {
    const dept = e.currentTarget.dataset.info;
    this.setData({
      'SearchParams.LOU_NO': dept.ID,
      'SearchParams.LOU_NAME': dept.Label,
      BuildingShow: false
    });
  },
  inputChange(e) {
    const type = e.currentTarget.dataset.type;
    let val = e.detail.value;
    this.setData({
      ['SearchParams.' + type]: val
    });
  },
  UserTypeCancel() {
    this.setData({
      UserTypeShow: false
    });
  },
  UserTypeConfirm(e) {
    const {
      value,
      index
    } = e.detail;
    this.setData({
      'SearchParams.CONS_SORT_CODE': value.value,
      'SearchParams.CONS_SORT_NAME': value.name,
      UserTypeShow: false,
      UserTypeActive: index
    });
  },

  WATEROFF_CODECancel() {
    this.setData({
      UseHeatStateShow: false
    });
  },
  WATEROFF_CODEConfirm(e) {
    const {
      value,
      index
    } = e.detail;
    this.setData({
      'SearchParams.WATEROFF_CODE': value.value,
      'SearchParams.WATEROFF_CODE_STR': value.name,
      UseHeatStateShow: false,
      UseHeatStateActive: index
    });
  },

  IsOweCancel() {
    this.setData({
      IsOweShow: false
    });
  },
  IsOweConfirm(e) {
    const {
      value,
      index
    } = e.detail;
    this.setData({
      'SearchParams.ISOWE': value.value,
      'SearchParams.ISOWE_NAME': value.name,
      IsOweShow: false,
      IsOweActive: index
    });
  },
  reset() {
    this.setData({
      SearchParams: {
        DEPT_NO: '',
        DEPT_NAME: '',
        LOU_NO: '',
        LOU_NAME: '',
        UNIT: '',
        CONS_NO: '',
        CONS_NAME: '',
        CONS_SORT_CODE: '',
        CONS_SORT_NAME: '',
        WATEROFF_CODE: '',
        WATEROFF_CODE_STR: '',
        PHONE: '',
        ISOWE: '',
        ISOWE_NAME: '',
      },
    })
  },
  doSearch() {
    this.setData({
      pagination: {
        page: 1,
        rows: 30,
        sidx: 't.ORDER_CODE',
        sord: ''
      },
      HasNextPage: true,
      noData: false,
      isReachBottom: false,
      ShowPop: false
    });
    this.searchUser(true);
  },
  toUser(e){
    var user = e.currentTarget.dataset.user;
    wx.navigateTo({
      url: "/packageAudit/user-info/user-info",
      success: function(res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('sendmessage', { data: user })
      }
    })
  }
})